<?php
namespace App\Http\Controllers;

use App\Traits\Msg;
use Illuminate\Http\Request;
use zgldh\QiniuStorage\QiniuStorage;


class PublicController extends Controller
{
    use Msg;
    //图片上传处理
    public function uploadImg(Request $request)
    {
        $data = ['code'=>1, 'msg'=>'上传失败', 'url'=>''];
        if ($request->hasFile('file') && $request->file('file')->isValid()) {
            $maxSize = 1;
            //支持的上传图片类型
            $allowed_extensions = ["png", "jpg", "gif","jpeg"];
            $file = $request->file('file');
            $ext = $file->getClientOriginalExtension();
            if (!in_array(strtolower($ext),$allowed_extensions)){
                $data['msg'] = "请上传".implode(",",$allowed_extensions)."格式的图片";
                return response()->json($data);
            }
            //检测图片大小
            if ($file->getClientSize() > $maxSize*1024*1024){
                $data['msg'] = "图片大小限制".$maxSize."M";
                return response()->json($data);
            }
            $store_result = $file->store('public/'.date('Ymd'));
            if($store_result){
                $data = [
                    'code'  => 0,
                    'msg'   => '上传成功',
                    'url'  =>substr($store_result,7)
                ];
                return response()->json($data);
            }
        }
        $data['msg'] = "未获取到上传文件或上传过程出错";
        return response()->json($data);
    }
}